package com.forexample.myapplication

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.navigation.ui.AppBarConfiguration
import com.forexample.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private var permissionCamera: String = Manifest.permission.CAMERA
    private val requestCode = 10086;
    private var serviceIntent: Intent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        serviceIntent = Intent(this@MainActivity, CameraService::class.java)
        ActivityCompat.requestPermissions(
            this, arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,

                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
            ), requestCode
        )

        binding.fabFront.setOnClickListener {

            if (Settings.canDrawOverlays(this)) {
                if (!OpenServiceState.isOpen) {
                    serviceIntent!!.putExtra("LENS_FACING", "LENS_FACING_FRONT")
                    startService(serviceIntent)
                    OpenServiceState.isOpen=true;
                }

            } else {
                val premissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                startActivity(premissionIntent)
            }

        }
        binding.fabAfter.setOnClickListener {
            if (Settings.canDrawOverlays(this)) {
                if (!OpenServiceState.isOpen) {
                    serviceIntent!!.putExtra("LENS_FACING", "LENS_FACING_BACK")
                    startService(serviceIntent)
                    OpenServiceState.isOpen=true;
                }

            } else {
                val premissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                startActivity(premissionIntent)
            }
        }

        binding.fabStop.setOnClickListener {
            stopService(serviceIntent)
        }
    }



    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == this@MainActivity.requestCode) {
            for (i: Int in grantResults) {
            }
        }
    }

    companion object OpenServiceState{
        public var isOpen:Boolean=false;
    }
}